import { ref, onMounted } from 'vue'
import { solarToLunar } from 'vue-solar-lunar';
import data from '@/TestData/hos.json'
function getFirstDayOfMonth(year, month) {
    const date = new Date(year, month - 1, 1)
    return date.getDay()
}

function getDaysInPreviousMonth(year, month) {
    // 如果 month 是 1，我们需要将年份减 1 并设置 month 为 12 来获取上一年的最后一个月的天数  
    if (month === 1) {
        year--;
        month = 12;
    }else{
        month--;
    }
    return new Date(year, month, 0).getDate();
}
function getweek(year, month, day) {
    const date = new Date(year, month - 1, day)
    const week = date.getDay()
    return week
}
//更新节假日属性
function updateHolidays( days,year,month){
    //console.log(data.result.data.holiday_array)
    const holiday_array = data.result.data.holiday_array
    const holidays=[]
    holiday_array.forEach(item => {
        item.list.forEach(holiday => {
            holidays.push(holiday)
        })
    });
    //console.log(holidays)
    for(let i=0;i<days.length;i++){
        for(let j=0;j<holidays.length;j++){
            if(days[i].date === holidays[j].date){
                days[i].holidays = holidays[j].status
            }
        }
    }
    return days
}
//默认周六日为休息日
function updateWeekend(year,month,days){
   let week=getweek(year, month, days)
    if(week===0 || week===6){
        return '1'
    }else{
        return '2'
    }
}
export function getToday() {
    let time=''
    const today = new Date()
    const year = today.getFullYear()
    const month = today.getMonth() + 1
    const day = today.getDate()
    time = year + '-' + month + '-' + day
    return time
    
}

 export function getMonthDays(year, month) {
    //console.log(year, month)
    const daysInMonth = new Date(year, month, 0).getDate()
   // console.log(daysInMonth)
    const firstDayOfMonth = getFirstDayOfMonth(year, month)
    const daysInPreviousMonth = getDaysInPreviousMonth(year, month)
    const PreviousMonthDays = []
    const CurrentMonthDays = []
    const NextMonthDays = []
    let lastyear = month === 1 ? year - 1 : year
    let lastmonth= month === 1 ? 12 : month - 1
    let nextyear = month === 12 ? year + 1 : year
    let nextmonth= month=== 12 ? 1 : month + 1
    //console.log(lastyear,lastmonth,nextyear,nextmonth)
    for (let i = firstDayOfMonth - 1; i >= 0; i--) {
        PreviousMonthDays.push(
            {
                day: daysInPreviousMonth - i,
                mouth: lastmonth,
                year: lastyear,
                type: 'prev',
                date: `${lastyear}-${lastmonth}-${daysInPreviousMonth - i}`,
                LunarDay :solarToLunar(lastyear,lastmonth, daysInPreviousMonth - i),
                holidays:updateWeekend(lastyear,lastmonth, daysInPreviousMonth - i),
                week: getweek(lastyear, lastmonth, daysInPreviousMonth - i)
            })
    }
   
    for(let i = 1; i <= daysInMonth; i++) {
        CurrentMonthDays.push(
            {
                day: i,
                mouth: month,
                year: year,
                type: 'current',
                date: `${year}-${month}-${i}`,
                LunarDay :solarToLunar(year, month, i),
                holidays:updateWeekend(year,month,i),
                week: getweek(year, month, i)
            })
    }
    let numDaysNextMonth = 42 - PreviousMonthDays.length - CurrentMonthDays.length
    for (let i = 1; i <= numDaysNextMonth; i++) {
        NextMonthDays.push(
            {
                day: i,
                mouth: nextmonth,
                year: nextyear,
                type: 'next',
                date: `${nextyear}-${nextmonth}-${i}`,
                LunarDay :solarToLunar(nextyear, nextmonth, i),
                holidays:updateWeekend(nextyear, nextmonth, i),
                week: getweek(nextyear, nextmonth, i)
            })
    }
    let days = PreviousMonthDays.concat(CurrentMonthDays, NextMonthDays)
    days  = updateHolidays(days,year,month)
   
    return days

}

